package com.att.service.impl;

import com.att.bean.NumRunDeilBean;
import com.att.dao.NumRunDeilDaoMapper;
import com.att.service.NumRunDeilService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service("numRunDeilService")
public class NumRunDeilServiceImpl implements NumRunDeilService {

    @Resource
    private NumRunDeilDaoMapper numRunDeilDaoMapper;

    @Override
    public List<List<NumRunDeilBean>> findByCondition(NumRunDeilBean bean) {
        List<NumRunDeilBean> list = numRunDeilDaoMapper.findByCondition(bean);
        List<List<NumRunDeilBean>> arrayList = new ArrayList<>();
        Map<Integer, List<NumRunDeilBean>> map = new HashMap<>();
        for (NumRunDeilBean numRunDeilBean :list){
            List<NumRunDeilBean> tempList = map.get(numRunDeilBean.getRunSort());
            if(tempList == null){
                tempList = new ArrayList<>();
                tempList.add(numRunDeilBean);
                map.put(numRunDeilBean.getRunSort(), tempList);
            }else {
                tempList.add(numRunDeilBean);
            }
        }
        Iterator<Map.Entry<Integer, List<NumRunDeilBean>>> it = map.entrySet().iterator();
        while(it.hasNext()){
                Map.Entry<Integer, List<NumRunDeilBean>> entry = it.next();
                arrayList.add(entry.getValue());
        }
        return arrayList;
    }

    @Override
    public List<NumRunDeilBean> findNumRunDeilByNumRunId(NumRunDeilBean bean) {
        return numRunDeilDaoMapper.findNumRunDeilByNumRunId(bean);
    }

    @Override
    public List<NumRunDeilBean> findNumRunDeilBean(NumRunDeilBean bean) {
        return numRunDeilDaoMapper.findNumRunDeilBean(bean);
    }

    @Override
    public List<NumRunDeilBean> findNumRunDeilById(NumRunDeilBean bean) {
        return numRunDeilDaoMapper.findNumRunDeilById(bean);
    }

    @Override
    public boolean insert(NumRunDeilBean bean) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        List list = new ArrayList();
        String[] sDays = bean.getStartDays().split(",");
        for (int i = 0;i < sDays.length;i++){
            NumRunDeilBean numRunDeilBean = new NumRunDeilBean();
            numRunDeilBean.setNumRunId(bean.getNumRunId());
//            if(bean.getStartTimeStr() != null && bean.getStartTimeStr() != ""){
                String[] startTimeStr = bean.getStartTimeStr().split(",");
                numRunDeilBean.setStartTime(format.parse(startTimeStr[i]));
                numRunDeilBean.setEndTime(format.parse(startTimeStr[i]));
                numRunDeilBean.setDeilType(bean.getDeilType());
//            }else {
//                numRunDeilBean.setStartTime(bean.getStartTime());
//                numRunDeilBean.setEndTime(bean.getEndTime());
//                numRunDeilBean.setDeilType(1);
//            }
            numRunDeilBean.setSchClassId(bean.getSchClassId());
            numRunDeilBean.setAddWorkTime(bean.getAddWorkTime());
            numRunDeilBean.setIsAddWork(bean.getIsAddWork());
            Integer sdayId = Integer.valueOf(sDays[i]);
            numRunDeilBean.setsDays(sdayId);
            if(bean.getEndDays() != null && bean.getEndDays() != ""){
                String[] eDays = bean.getEndDays().split(",");
                Integer edayId = Integer.valueOf(eDays[i]);
                numRunDeilBean.seteDays(edayId);
            }else {
                numRunDeilBean.seteDays(sdayId);
            }
            list.add(numRunDeilBean);
        }
        return numRunDeilDaoMapper.insert(list);
    }

    @Override
    public boolean deleteTempDeil(NumRunDeilBean bean) {
        return numRunDeilDaoMapper.deleteTempDeil(bean);
    }

    @Override
    public boolean delete(NumRunDeilBean bean) {
        return numRunDeilDaoMapper.delete(bean);
    }
}
